//
//  MZColorDemoView.m
//  MZBaseDemo
//
//  Created by Chris Yang on 2016/12/2.
//  Copyright © 2016年 Chris Yang. All rights reserved.
//

#import "UIView+MZLayout.h"
#import "MZColorButton.h"

#import "MZColorDemoView.h"

@implementation MZColorDemoView

- (id)init {
    
    self = [super init];
    
    if (self) {
        [self configSelf];
        [self allocSubviews];
        [self configSubviews];
        [self positionSubviews];
    }
    
    return self;
}

- (void)configSelf {
    [self setBackgroundColor:[UIColor whiteColor]];
}

- (void)allocSubviews {
    _scrollView = [[UIScrollView alloc] init];
    _noteLabel = [[UILabel alloc] init];
}

- (void)configSubviews {
    
    /* Color Buttons */
    for (int i = 0; i < _colorButtonArray.count; i++) {
        MZColorButton *tmpButton = _colorButtonArray[i];
        [tmpButton setTitle:@"" withColor:[UIColor blackColor]];
        tmpButton.tag = i;
        [tmpButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    /* Note Label */
    [_noteLabel setText:@"点击上面的项目以测试效果"];
    [_noteLabel setTextColor:[UIColor blackColor]];
    [_noteLabel setFont:[UIFont systemFontOfSize:30]];
    [_noteLabel setTextAlignment:NSTextAlignmentCenter];
    [_noteLabel setAdjustsFontSizeToFitWidth:YES];

    [self addSubview:_scrollView];
    for (MZColorButton *tmpButton in _colorButtonArray) {
        [_scrollView addSubview:tmpButton];
    }
    [_scrollView addSubview:_noteLabel];
}

- (void)positionSubviews {
    
    CGRect tmpRect = CGRectZero;
    
    /* Scroll view */
    tmpRect = self.bounds;
    [_scrollView setFrame:tmpRect];
    
    tmpRect = [_scrollView mzCenterRectInSubviewWithWidth:300 height:40 toTop:80];
    /* Color Buttons */
    for (MZColorButton *tmpButton in _colorButtonArray) {
        [tmpButton setFrame:tmpRect];
        tmpRect.origin.y += (tmpRect.size.height + 10);
    }
    
    /* Note Label */
    tmpRect.origin.y += 20;
    [_noteLabel setFrame:tmpRect];
    
    /* Scollview ContentSize */
    tmpRect.size.width = _scrollView.bounds.size.width;
    tmpRect.size.height = MAX(_scrollView.bounds.size.height + 1, [_noteLabel mzGetYForBrotherViewToTopWithSpace:10]);
    [_scrollView setContentSize:tmpRect.size];
}

- (void)layoutSubviews {
    [self positionSubviews];
}

#pragma mark - Target-Action Pair

- (void)buttonClicked:(UIButton *)button {
    
    if ([_delegate respondsToSelector:@selector(colorView:colorButtonClickedAtIndex:)]) {
        [_delegate colorView:self colorButtonClickedAtIndex:button.tag];
    }
}

#pragma mark - MISC

#pragma mark - User Interface

- (void)resetWithTitlePrefixArray:(NSArray<NSString *> *)titlePrefixArray {
    
    for (MZColorButton *tmpButton in _colorButtonArray) {
        [tmpButton removeFromSuperview];
    }
    
    NSMutableArray *tmpArray = [[NSMutableArray alloc] init];
    for (NSString *tmpPrefix in titlePrefixArray) {
        MZColorButton *tmpButton = [[MZColorButton alloc] init];
        [tmpButton setTitlePrefix:tmpPrefix];
        
        [tmpArray addObject:tmpButton];
    }
    _colorButtonArray = [NSArray arrayWithArray:tmpArray];
    
    [self configSubviews];
    [self positionSubviews];
}

- (void)setButtonTitle:(NSString *)title
                 color:(UIColor *)color
               atIndex:(NSInteger)index {
    
    if (index < 0 || index >= _colorButtonArray.count) {
        return;
    }
    
    MZColorButton *tmpButton = _colorButtonArray[index];
    [tmpButton setTitle:title withColor:color];
}

@end



















